/// 应用设置数据模型
/// Application settings data model
/// 
/// 用于存储和管理应用的各种设置选项
/// Used to store and manage various application setting options
library;

import 'package:flutter/material.dart';

class AppSettings {
  /// 设置键
  /// Settings key
  final String key;
  
  /// 设置值
  /// Settings value
  final String value;
  
  /// 更新时间
  /// Update time
  final DateTime updatedAt;

  const AppSettings({
    required this.key,
    required this.value,
    required this.updatedAt,
  });

  /// 从JSON创建实例
  /// Create instance from JSON
  factory AppSettings.fromJson(Map<String, dynamic> json) {
    return AppSettings(
      key: json['key'] as String,
      value: json['value'] as String,
      updatedAt: DateTime.fromMillisecondsSinceEpoch(json['updated_at'] as int),
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'key': key,
      'value': value,
      'updated_at': updatedAt.millisecondsSinceEpoch,
    };
  }

  /// 复制并修改部分属性
  /// Copy with modified properties
  AppSettings copyWith({
    String? key,
    String? value,
    DateTime? updatedAt,
  }) {
    return AppSettings(
      key: key ?? this.key,
      value: value ?? this.value,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is AppSettings && other.key == key;
  }

  @override
  int get hashCode => key.hashCode;

  @override
  String toString() {
    return 'AppSettings(key: $key, value: $value, updatedAt: $updatedAt)';
  }
}

/// 设置键常量
/// Settings key constants
class SettingsKeys {
  static const String themeMode = 'theme_mode';
  static const String notificationEnabled = 'notification_enabled';
  static const String notificationTime = 'notification_time';
  static const String fontSize = 'font_size';
  static const String firstLaunch = 'first_launch';
  static const String language = 'language';
  static const String autoBackup = 'auto_backup';
  static const String reminderFrequency = 'reminder_frequency';
  static const String biometricEnabled = 'biometric_enabled';
  static const String analyticsEnabled = 'analytics_enabled';
  static const String crashReportingEnabled = 'crash_reporting_enabled';
}

/// 主题模式枚举
/// Theme mode enumeration
enum AppThemeMode {
  light('light', '浅色模式'),
  dark('dark', '深色模式'),
  system('system', '跟随系统');

  const AppThemeMode(this.value, this.label);

  /// 存储值
  /// Storage value
  final String value;
  
  /// 显示标签
  /// Display label
  final String label;

  /// 从字符串值创建枚举
  /// Create enum from string value
  static AppThemeMode fromString(String value) {
    return AppThemeMode.values.firstWhere(
      (mode) => mode.value == value,
      orElse: () => AppThemeMode.light,
    );
  }

  /// 转换为Flutter ThemeMode
  /// Convert to Flutter ThemeMode
  ThemeMode toThemeMode() {
    switch (this) {
      case AppThemeMode.light:
        return ThemeMode.light;
      case AppThemeMode.dark:
        return ThemeMode.dark;
      case AppThemeMode.system:
        return ThemeMode.system;
    }
  }

  /// 从Flutter ThemeMode创建
  /// Create from Flutter ThemeMode
  static AppThemeMode fromThemeMode(ThemeMode mode) {
    switch (mode) {
      case ThemeMode.light:
        return AppThemeMode.light;
      case ThemeMode.dark:
        return AppThemeMode.dark;
      case ThemeMode.system:
        return AppThemeMode.system;
    }
  }
}

/// 字体大小枚举
/// Font size enumeration
enum FontSize {
  small('small', '小', 12.0),
  medium('medium', '中', 14.0),
  large('large', '大', 16.0),
  extraLarge('extra_large', '特大', 18.0);

  const FontSize(this.value, this.label, this.size);

  /// 存储值
  /// Storage value
  final String value;
  
  /// 显示标签
  /// Display label
  final String label;
  
  /// 字体大小值
  /// Font size value
  final double size;

  /// 从字符串值创建枚举
  /// Create enum from string value
  static FontSize fromString(String value) {
    return FontSize.values.firstWhere(
      (size) => size.value == value,
      orElse: () => FontSize.medium,
    );
  }
}

/// 提醒频率枚举
/// Reminder frequency enumeration
enum ReminderFrequency {
  daily('daily', '每天'),
  weekly('weekly', '每周'),
  monthly('monthly', '每月'),
  never('never', '从不');

  const ReminderFrequency(this.value, this.label);

  /// 存储值
  /// Storage value
  final String value;
  
  /// 显示标签
  /// Display label
  final String label;

  /// 从字符串值创建枚举
  /// Create enum from string value
  static ReminderFrequency fromString(String value) {
    return ReminderFrequency.values.firstWhere(
      (freq) => freq.value == value,
      orElse: () => ReminderFrequency.daily,
    );
  }
}

/// 备份频率枚举
/// Backup frequency enumeration
enum BackupFrequency {
  daily('daily', '每天'),
  weekly('weekly', '每周'),
  monthly('monthly', '每月'),
  never('never', '从不');

  const BackupFrequency(this.value, this.label);

  /// 存储值
  /// Storage value
  final String value;
  
  /// 显示标签
  /// Display label
  final String label;

  /// 从字符串值创建枚举
  /// Create enum from string value
  static BackupFrequency fromString(String value) {
    return BackupFrequency.values.firstWhere(
      (freq) => freq.value == value,
      orElse: () => BackupFrequency.weekly,
    );
  }
}

/// 锁定类型枚举
/// Lock type enumeration
enum LockType {
  none('none', '无'),
  pin('pin', 'PIN码'),
  password('password', '密码'),
  biometric('biometric', '生物识别');

  const LockType(this.value, this.label);

  /// 存储值
  /// Storage value
  final String value;
  
  /// 显示标签
  /// Display label
  final String label;

  /// 从字符串值创建枚举
  /// Create enum from string value
  static LockType fromString(String value) {
    return LockType.values.firstWhere(
      (type) => type.value == value,
      orElse: () => LockType.none,
    );
  }
}